package org.esti.model.dao.impl;

import org.esti.model.Appraisal;
import org.esti.model.dao.AppraisalDAO;
import org.esti.model.exceptions.AppraisalDoesNotExistException;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * @author Artem Loginov (logart)
 */
@Repository
public class AppraisalDAOimpl implements AppraisalDAO {

    @PersistenceContext
    private EntityManager entityManager;

    /**
     * {@inheritDoc}
     */
    @Override
    public Appraisal getAppraisalByLevelId(long levelId) {
        try {
            Query query = entityManager.createQuery(
                    "FROM Appraisal WHERE level_id=:level_id"
            );
            query.setParameter("level_id", levelId);
            return (Appraisal) query.getSingleResult();
        } catch (NoResultException ex) {
            throw new AppraisalDoesNotExistException(ex);
        }
    }
}
